package com.ruoyi.web.controller.pc.uc;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.page.PageUtils;
import com.ruoyi.common.validator.group.AddGroup;
import com.ruoyi.common.validator.group.UpdateGroup;
import com.ruoyi.web.domain.UcQuoQuota;
import com.ruoyi.web.domain.UcQuoQuotaOption;
import com.ruoyi.web.domain.vo.QueryVo;
import com.ruoyi.web.mapper.UcQuoQuotaOptionMapper;
import com.ruoyi.web.service.IUcQuoQuotaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/userConfig/ucQuoQuota")
public class UcQuoQuotaController extends BaseController {

    @Autowired
    private IUcQuoQuotaService ucQuoQuestionService;

    @Autowired
    private UcQuoQuotaOptionMapper ucQuoQuotaOptionMapper;

    @PreAuthorize("@ss.hasPermi('userConfig:ucQuoQuota:list')")
    @GetMapping("/list")
    public TableDataInfo list(QueryVo queryVo) {
        PageUtils page = ucQuoQuestionService.listByPage(queryVo);
        return getDataTable(page);
    }


    @PreAuthorize("@ss.hasPermi('userConfig:ucQuoQuota:add')")
    @Log(title = "指标配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated(AddGroup.class) @RequestBody UcQuoQuota ucQuoQuota){
        return toAjax(ucQuoQuestionService.insert(ucQuoQuota));
    }

    @PreAuthorize("@ss.hasPermi('userConfig:ucQuoQuota:edit')")
    @Log(title = "指标配置", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@Validated(UpdateGroup.class) @RequestBody UcQuoQuota ucQuoQuota) {
        return toAjax(ucQuoQuestionService.update(ucQuoQuota));
    }

    @GetMapping(value = "/info/{id}")
    public AjaxResult info(@PathVariable("id") Long id) {

        UcQuoQuota ucQuoQuota = ucQuoQuestionService.getBaseMapper().selectById(id);
        QueryWrapper<UcQuoQuotaOption> qwUcQuoQuotaOption = new QueryWrapper<>();
        qwUcQuoQuotaOption.eq("quota_id",ucQuoQuota.getId());
        List<UcQuoQuotaOption> ucQuoQuotaOptions = ucQuoQuotaOptionMapper.selectList(qwUcQuoQuotaOption);
        ucQuoQuota.setUcQuoQuotaOptions(ucQuoQuotaOptions);
        return success(ucQuoQuota);
    }

    @PostMapping("/getOptions")
    public AjaxResult getOptions(@RequestBody UcQuoQuota ucQuoQuota){
        QueryWrapper<UcQuoQuotaOption> qwUcQuoQuotaOption = new QueryWrapper<>();
        qwUcQuoQuotaOption.eq("quota_id",ucQuoQuota.getId());
        List<UcQuoQuotaOption> ucQuoQuotaOptions = ucQuoQuotaOptionMapper.selectList(qwUcQuoQuotaOption);
        return success(ucQuoQuotaOptions);
    }




}

